package com.task.core.service.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import com.task.common.core.redis.RedisCache;
import com.task.common.enums.QuestionOperationTypeEnum;
import com.task.common.redis.RedisKeyConstants;
import com.task.common.utils.StringUtils;
import com.task.core.service.PluginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.task.common.annotation.DataScope;
import com.task.common.domain.info.KeyAndValue;
import com.task.common.enums.QuestionInfoTypeEnum;
import com.task.common.exception.ServiceException;
import com.task.common.utils.SecurityUtils;
import com.task.common.utils.uuid.UUID;
import com.task.core.domain.PluginPlatformInfo;
import com.task.core.dto.request.PluginPlatformRequestDTO;
import com.task.core.dto.request.QuestionInfoRequestDTO;
import com.task.core.mapper.PluginPlatformMapper;
import com.task.core.service.PluginPlatformService;
import com.task.core.service.QuestionInfoService;
import org.springframework.util.ObjectUtils;

/**
 * @author 迪迦.
 * @date 2024/11/4 17:28
 */
@Service("PluginPlatformService")
public class PluginPlatformServiceImpl implements PluginPlatformService {

  @Autowired
  private PluginPlatformMapper pluginPlatformMapper;

  @Resource
  private QuestionInfoService questionInfoService;

  @Resource
  private RedisCache redisCache;

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<PluginPlatformInfo> getPluginPlatformByPage(PluginPlatformRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return pluginPlatformMapper.getPluginPlatformByPage(request);
  }

  @Override
  //@DataScope(deptAlias = "dept", userAlias = "su")
  public List<KeyAndValue> getPluginPlatformForOption(PluginPlatformRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return pluginPlatformMapper.getPluginPlatformForOption(request);
  }

  @Override
  public void addPluginPlatform(PluginPlatformRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    request.setCode(UUID.randomUUID().toString().replace("-", ""));
    request.setVersionCode(UUID.randomUUID().toString().replace("-", ""));
    pluginPlatformMapper.addPluginPlatform(request);
  }

  @Override
  public void modifyPluginPlatform(PluginPlatformRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    request.setVersionCode(UUID.randomUUID().toString().replace("-", ""));
    Integer integer = pluginPlatformMapper.modifyPluginPlatform(request);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
    List<String> pluginCodes = pluginPlatformMapper.getPlugCodeByPluginPlatformCode(request.getCode());
    if(!ObjectUtils.isEmpty(pluginCodes)){
      List<String> keys = new ArrayList<>();
      for (String pluginCode : pluginCodes) {
        keys.add(RedisKeyConstants.PLUGIN_INFO.formatKey(pluginCode));
      }
      redisCache.redisTemplate.delete(keys);
    }
    Boolean isForcePush = request.getIsForcePush();
    if(!ObjectUtils.isEmpty(isForcePush) && isForcePush){
      questionInfoService.insertQuestion(new QuestionInfoRequestDTO(QuestionInfoTypeEnum.PLUGIN, request.getCode(), "",request.getPromptCode(), QuestionOperationTypeEnum.UPDATA.getCode()));
    }
  }

  @Override
  public void deletePluginPlatform(String pluginPlatformCode, Long userId) {
    Integer integer = pluginPlatformMapper.deletePluginPlatform(pluginPlatformCode, userId);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
    questionInfoService.insertQuestion(new QuestionInfoRequestDTO(QuestionInfoTypeEnum.PLUGIN, pluginPlatformCode, "",QuestionOperationTypeEnum.DELETE.getCode()));
  }

  @Override
  public PluginPlatformInfo getPluginPlatformById(String pluginPlatformCode) {
    return pluginPlatformMapper.getPluginPlatformById(pluginPlatformCode);
  }
}
